package handler;

import service.StudentService;
import service.impl.StudentServiceImpI;
import view.AddStudentView;
import view.MainView;
import view.StudentMainView;
import view.UpdateStudentView;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class StudentMainViewHandler implements ActionListener {

    private StudentMainView studentMainView;

    public StudentMainViewHandler(StudentMainView studentMainView) {
        this.studentMainView = studentMainView;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton = (JButton) e.getSource();
        String text = jButton.getText();
        studentMainView.reloadTable();

//        if ("增加".equals(text)) {
//            new AddStudentView(studentMainView);
//
//        } else if ("修改".equals(text)) {
//            int[] selectStudentIds = studentMainView.getSelectStudentIds();
//            if (selectStudentIds.length != 1) {
//                JOptionPane.showMessageDialog(studentMainView, "一次只能修改一个");
//                return;
//            }
//            new UpdateStudentView(studentMainView, selectStudentIds[0]);
//        } else if ("删除".equals(text)) {
//            int[] selectStudentIds = studentMainView.getSelectStudentIds();
//            if (selectStudentIds.length == 0) {
//                JOptionPane.showMessageDialog(studentMainView, "请选择要删除的行");
//                return;
//            }
//            int option = JOptionPane.showConfirmDialog(studentMainView, "确认删除所选择的" + selectStudentIds.length + "行吗？", "确认删除", JOptionPane.YES_NO_OPTION);
//            if (option == JOptionPane.YES_OPTION) {//确认
//                //执行删除
//                StudentService studentService = new StudentServiceImpI();
//                boolean deleteResult = studentService.delete(selectStudentIds);
//                if (deleteResult) {
//                    //重新加载表格
//                    studentMainView.reloadTable();
//                    //updateStudentView.dispose();
//                } else {
//                    JOptionPane.showMessageDialog(studentMainView, "删除失败");
//                }
//            }
//        }
        if ("查询".equals(text)) {
            studentMainView.setPageNow(1);
            studentMainView.reloadTable();
        } else if ("上一页".equals(text)) {
            studentMainView.setPageNow(studentMainView.getPageNow() - 1);
            studentMainView.reloadTable();
        } else if ("下一页".equals(text)) {
            studentMainView.setPageNow(studentMainView.getPageNow() + 1);
            studentMainView.reloadTable();
        }
    }
}
